package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.UserRule;
import com.ruoyi.system.service.IUserRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 考勤规则Controller
 * 
 * @author ruoyi
 * @date 2025-05-20
 */
@RestController
@RequestMapping("/system/user_role")
public class UserRuleController extends BaseController
{
    @Autowired
    private IUserRuleService userRuleService;

    /**
     * 查询考勤规则列表
     */
    @PreAuthorize("@ss.hasPermi('system:user_role:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserRule userRule)
    {
        startPage();
        List<UserRule> list = userRuleService.selectUserRuleList(userRule);
        return getDataTable(list);
    }

    /**
     * 导出考勤规则列表
     */
    @PreAuthorize("@ss.hasPermi('system:user_role:export')")
    @Log(title = "考勤规则", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserRule userRule)
    {
        List<UserRule> list = userRuleService.selectUserRuleList(userRule);
        ExcelUtil<UserRule> util = new ExcelUtil<UserRule>(UserRule.class);
        util.exportExcel(response, list, "考勤规则数据");
    }

    /**
     * 获取考勤规则详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:user_role:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(userRuleService.selectUserRuleById(id));
    }

    /**
     * 新增考勤规则
     */
    @PreAuthorize("@ss.hasPermi('system:user_role:add')")
    @Log(title = "考勤规则", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserRule userRule)
    {
        return toAjax(userRuleService.insertUserRule(userRule));
    }

    /**
     * 修改考勤规则
     */
    @PreAuthorize("@ss.hasPermi('system:user_role:edit')")
    @Log(title = "考勤规则", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserRule userRule)
    {
        return toAjax(userRuleService.updateUserRule(userRule));
    }

    /**
     * 删除考勤规则
     */
    @PreAuthorize("@ss.hasPermi('system:user_role:remove')")
    @Log(title = "考勤规则", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(userRuleService.deleteUserRuleByIds(ids));
    }
}
